home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 004 / bm / execute.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  88 lines

  1. #include <stdio.h>
  2. #include "bm.h"
  3. #include "Extern.h"
  4. Execute(DescVec, NPats, TextFile, Buffer)
  5. struct PattDesc *DescVec[]; /* pointers to status vectors for the different
  6.     * patterns, including skip tables, position in buffer, etc. */
  7. int NPats; /* number of patterns */
  8. char Buffer[]; /* holds text from file */
  9. int TextFile; /* file to search */
  10. {
  11.     int NRead, /* number of chars read from file */
  12.         NWanted, /* number of chars wanted */
  13.         NAvail, /* number of chars actually read */
  14.         BuffSize, /* number of chars in buffer */
  15.         BuffPos, /* offset of first char in Buffer in TextFile */
  16.         BuffEx, /* flag to indicate that buffer has been searched */
  17.         ResSize,
  18.         /* number of characters in the last, incomplete line */
  19.         Found, /* flag indicates whether pattern found
  20.         * completely and all matches printed */
  21.         Valid; /* was the match "valid", i.e. if -x used,
  22.         * did the whole line match? */
  23.     char *BuffEnd; /* pointer to last char of last complete line */
  24.  
  25.     /* misc working variables */
  26.     int i;
  27.  
  28.     /* initialize */
  29.     ResSize = 0;
  30.     Found = 0;
  31.     BuffPos = 0;
  32.     for (i=0; i < NPats; i++) {
  33.         DescVec[i] -> Success = 0;
  34.         DescVec[i] -> Start = Buffer;
  35.     } /* for */
  36.     /* now do the searching */
  37.     do {
  38.         /* first, read a bufferfull and set up the variables */
  39.         NWanted = MAXBUFF - ResSize; NRead = 0;
  40.         do {
  41.             NAvail =
  42.                read(TextFile,Buffer + ResSize + NRead, NWanted);
  43.             if (NAvail == -1) {
  44.                 fprintf(stderr,
  45.                   "bm: error reading from input file\n");
  46.                 exit(2);
  47.             } /* if */
  48.             NRead += NAvail; NWanted -= NAvail;
  49.         } while (NAvail && NWanted);
  50.         BuffEx = 0;
  51.         BuffSize = ResSize + NRead;
  52.         BuffEnd = Buffer + BuffSize - 1;
  53.         /* locate the end of the last complete line */
  54.         while (*BuffEnd != '\n' && BuffEnd >= Buffer)
  55.             --BuffEnd;
  56.         if (BuffEnd < Buffer)
  57.             BuffEnd = Buffer + BuffSize - 1;
  58.         while (!BuffEx) { /* work through one buffer full */
  59.             BuffEx = 1; /* set it provisionally, then clear
  60.             * it if we find the buffer non-empty */
  61.             for (i=0; i< NPats; i++) {
  62.                 if (!DescVec[i]->Success)
  63.                 /* if the pattern  has not been found */
  64.                     DescVec[i]-> Success =
  65.                     Search(DescVec[i]->Pattern,
  66.                     DescVec[i]->PatLen, Buffer, BuffEnd,
  67.                     DescVec[i]->Skip1, DescVec[i]->Skip2,
  68.                     DescVec[i]);
  69.                 if (DescVec[i]->Success){
  70.                 /* if a match occurred */
  71.                     BuffEx = 0;
  72.                     Valid = MatchFound(DescVec[i],BuffPos,
  73.                     Buffer, BuffEnd);
  74.                     Found |= Valid;
  75.                     if ((sFlag || lFlag) && Found)
  76.                         return(0);
  77.                 } /* if */
  78.             } /* for */
  79.         } /* while */
  80.         if(NRead) {
  81.             ResSize = MoveResidue(DescVec,NPats,Buffer,
  82.                 Buffer + BuffSize -1);
  83.             BuffPos += BuffSize - ResSize;
  84.         } /* if */
  85.     } while (NRead);
  86.     return(!Found);
  87. } /* Execute */
  88.